﻿using QuanLyGiaoVuService.ReportServices;
using QuanLyGiaoVuService.CommonServices;
using QuanLyGiaoVuUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyGiaoVuUI.Models.Mappers;
using QuanLyGiaoVuUI.Models.GiangVienClient;
using QuanLyGiaoVuUI.Models.GiangVien;
using QuanLyGiaoVuUI.Models.LopMonHoc;
using QuanLyGiaoVuService.CommandServices;
using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuUI.Models.NguoiDung;
using QuanLyGiaoVuService.Utilities;
using QuanLyGiaoVuUI.Extensions.ExtendedAttributes;
using QuanLyGiaoVuUI.Extensions.Utilities;

namespace QuanLyGiaoVuUI.Controllers
{
    [GiangVienAuthorize]
    [HandleError]
    public class GiangVienClientController : Controller
    {
        //
        // GET: /GiangVien/
        private IGiangVienReportService _giangVienReportService;
        private IGiangVienCommandService _giangVienCommandService;
        private ILopMonHocCommandService _lopMonHocCommandService;
        private ILopMonHocReportService _lopMonHocReportService;
        private IThamSoService _thamSoService;
        private IHocKyReportService _hocKyReportService;
        private IDangKyGioDayCommandService _dangKyGioDayCommandService;
        private IDangKyGioDayReportService _dangKyGioDayReportService;
        private INguoiDungCommandService _nguoiDungCommandService;
        private INguoiDungReportService _nguoiDungReportService;
        private ITietHocService _tietHocService;
        private IDeTaiCommandService _deTaiCommandService;
        private IDeTaiReportService _deTaiReportService;
        private IDangKyKetThucLopMonHocCommandService _dangKyKetThucLopMonHocCommandService;
        private IDangKyKetThucLopMonHocReportService _dangKyKetThucLopMonHocReportService;
        private ILichThiCommandService _lichThiCommandService;
        private ILichThiReportService _lichThiReportService;
        private IBanTinCommandService _banTinCommandService;
        private IBanTinReportService _banTinReportService;
        private ITienDoDeTaiCommandService _tienDoDeTaiCommandService;
        private ITienDoDeTaiReportService _tienDoDeTaiReportService;

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public GiangVienClientController(
            IGiangVienCommandService giangVienCommandService, 
            IGiangVienReportService giangVienReportService,
            IThamSoService thamSoservice,
            ILopMonHocReportService lopMonHocReportService,
            ILopMonHocCommandService lopMonHocCommandService, 
            IHocKyReportService hocKyReportService, 
            IDangKyGioDayReportService dangKyGioDayReportService, 
            IDangKyGioDayCommandService dangKyGioDayCommandService, 
            INguoiDungCommandService nguoiDungCommandService, 
            INguoiDungReportService nguoiDungReportService,
            IDeTaiCommandService deTaiCommandService,
            IDeTaiReportService deTaiReportService,
            ITietHocService tietHocService,
            IDangKyKetThucLopMonHocCommandService dangKyKetThucLopMonHocCommandService,
            IDangKyKetThucLopMonHocReportService dangKyKetThucLopMonHocReportService,
            ILichThiCommandService lichThiCommandService,
            ILichThiReportService lichThiReportService,
            IBanTinCommandService banTinCommandService,
            IBanTinReportService banTinReportService,
            ITienDoDeTaiCommandService tienDoDeTaiCommandService,
            ITienDoDeTaiReportService tienDoDeTaiReportService)
        {
            _giangVienReportService = giangVienReportService;
            _giangVienCommandService = giangVienCommandService;
            _thamSoService = thamSoservice;
            _lopMonHocReportService = lopMonHocReportService;
            _lopMonHocCommandService = lopMonHocCommandService;
            _hocKyReportService = hocKyReportService;
            _dangKyGioDayCommandService = dangKyGioDayCommandService;
            _dangKyGioDayReportService = dangKyGioDayReportService;
            _nguoiDungCommandService = nguoiDungCommandService;
            _nguoiDungReportService = nguoiDungReportService;
            _deTaiCommandService = deTaiCommandService;
            _deTaiReportService = deTaiReportService;
            _tietHocService = tietHocService;
            _dangKyKetThucLopMonHocCommandService = dangKyKetThucLopMonHocCommandService;
            _dangKyKetThucLopMonHocReportService = dangKyKetThucLopMonHocReportService;
            _lichThiCommandService = lichThiCommandService;
            _lichThiReportService = lichThiReportService;
            _banTinCommandService = banTinCommandService;
            _banTinReportService = banTinReportService;
            _tienDoDeTaiCommandService = tienDoDeTaiCommandService;
            _tienDoDeTaiReportService = tienDoDeTaiReportService;
        }

        public ActionResult Index()
        {
            NguoiDungDTO myInfo = UserUtility.GetCurrentUser();
            Guid id = myInfo.MaNguoiDung;
            ThongTinGiangVienClientViewModel thongtinGiangVienViewModel = new ThongTinGiangVienClientViewModel();
            thongtinGiangVienViewModel.DanhSachMonHocGiangDay = new List<MonHocDTO>();
            if (id != null && !id.Equals(Guid.Empty))
            {
                thongtinGiangVienViewModel.ID = (Guid)id;
                thongtinGiangVienViewModel = thongtinGiangVienViewModel.FromGiangVienDTO(_giangVienReportService.GetGiangVienById((Guid)id));
                thongtinGiangVienViewModel.DanhSachMonHocGiangDay = _giangVienReportService.GetAllMonHocByGiangVienId(thongtinGiangVienViewModel.ID);
            }
            thongtinGiangVienViewModel.DanhSachHocVi = _giangVienReportService.GetAllHocVi();
            thongtinGiangVienViewModel.DanhSachHocHam = _giangVienReportService.GetAllHocHam();
            thongtinGiangVienViewModel.DanhSachChucVu = _giangVienReportService.GetAllChucVu();
            return View(thongtinGiangVienViewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult CapNhat()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            CapNhatGiangVienClientViewModel capnhatGiangVienViewModel = new CapNhatGiangVienClientViewModel();
            capnhatGiangVienViewModel.DanhSachMonHocGiangDay = new List<MonHocDTO>();
            if (id != null && !id.Equals(Guid.Empty))
            {
                capnhatGiangVienViewModel.ID = (Guid)id;
                capnhatGiangVienViewModel = capnhatGiangVienViewModel.FromGiangVienDTO(_giangVienReportService.GetGiangVienById((Guid)id));
                capnhatGiangVienViewModel.DanhSachMonHocGiangDay = _giangVienReportService.GetAllMonHocByGiangVienId(capnhatGiangVienViewModel.ID);
            }
            //capnhatGiangVienViewModel.DanhSachHocVi = _giangVienReportService.GetAllHocVi();
            //capnhatGiangVienViewModel.DanhSachHocHam = _giangVienReportService.GetAllHocHam();
            //capnhatGiangVienViewModel.DanhSachChucVu = _giangVienReportService.GetAllChucVu();
            return View(capnhatGiangVienViewModel);
        }

        [HttpPost]
        [GiangVienAuthorize]
        public ActionResult CapNhat(CapNhatGiangVienClientViewModel _capNhatGiangVienClientViewModel)
        {
            CapNhatGiangVienClientViewModel capnhatGiangVienClientViewModel = new CapNhatGiangVienClientViewModel();
            try
            {
                //if (ModelState.IsValid)
                //{
                var giangvienDTO = _capNhatGiangVienClientViewModel.ToGiangVienDTO();

                //capnhatGiangVienClientViewModel.ID = _capNhatGiangVienClientViewModel.ID;
                capnhatGiangVienClientViewModel = capnhatGiangVienClientViewModel.FromGiangVienDTO(_giangVienReportService.GetGiangVienById(_capNhatGiangVienClientViewModel.ID));
                capnhatGiangVienClientViewModel.HoTenGV = _capNhatGiangVienClientViewModel.HoTenGV;
                capnhatGiangVienClientViewModel.GioiTinh = _capNhatGiangVienClientViewModel.GioiTinh;
                capnhatGiangVienClientViewModel.DiaChi = _capNhatGiangVienClientViewModel.DiaChi;
                capnhatGiangVienClientViewModel.SoDienThoai = _capNhatGiangVienClientViewModel.SoDienThoai;
                capnhatGiangVienClientViewModel.NgaySinh = _capNhatGiangVienClientViewModel.NgaySinh;
                capnhatGiangVienClientViewModel.Email = _capNhatGiangVienClientViewModel.Email;
                _giangVienCommandService.UpdateGiangVien(capnhatGiangVienClientViewModel.ToGiangVienDTO());
                // return RedirectToAction("index");
                // }
                //_capNhatGiangVienClientViewModel.DanhSachHocVi = _giangVienReportService.GetAllHocVi();
                //_capNhatGiangVienClientViewModel.DanhSachHocHam = _giangVienReportService.GetAllHocHam();
                //_capNhatGiangVienClientViewModel.DanhSachChucVu = _giangVienReportService.GetAllChucVu();

                return Json(new
                {
                    success = true
                });
            }
            catch (Exception exception)
            {
                log.Error(exception.Message, exception);
                return Json(new
                {
                    success = false,
                    message = exception.Message
                });
            }

            return View(_capNhatGiangVienClientViewModel);
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongTin()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            ThongTinGiangVienClientViewModel thongtinGiangVienViewModel = new ThongTinGiangVienClientViewModel();
            thongtinGiangVienViewModel.DanhSachMonHocGiangDay = new List<MonHocDTO>();
            if (id != null && !id.Equals(Guid.Empty))
            {
                thongtinGiangVienViewModel.ID = (Guid)id;
                GiangVienDTO gvDTO = _giangVienReportService.GetGiangVienById((Guid)id);
                thongtinGiangVienViewModel = thongtinGiangVienViewModel.FromGiangVienDTO(gvDTO);
                thongtinGiangVienViewModel.DanhSachMonHocGiangDay = _giangVienReportService.GetAllMonHocByGiangVienId(thongtinGiangVienViewModel.ID);
            }
            thongtinGiangVienViewModel.DanhSachHocVi = _giangVienReportService.GetAllHocVi();
            thongtinGiangVienViewModel.DanhSachHocHam = _giangVienReportService.GetAllHocHam();
            thongtinGiangVienViewModel.DanhSachChucVu = _giangVienReportService.GetAllChucVu();
            //thongtinGiangVienViewModel.ChucDanh=_g
            return View(thongtinGiangVienViewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongTinCoBan()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            ThongTinGiangVienClientViewModel thongtinGiangVienViewModel = new ThongTinGiangVienClientViewModel();
            thongtinGiangVienViewModel.DanhSachMonHocGiangDay = new List<MonHocDTO>();
            if (id != null && !id.Equals(Guid.Empty))
            {
                thongtinGiangVienViewModel.ID = (Guid)id;
                thongtinGiangVienViewModel = thongtinGiangVienViewModel.FromGiangVienDTO(_giangVienReportService.GetGiangVienById((Guid)id));                
            }
            return View(thongtinGiangVienViewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DamNhanMons()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            ThongTinGiangVienClientViewModel thongtinGiangVienViewModel = new ThongTinGiangVienClientViewModel();
            thongtinGiangVienViewModel.DanhSachMonHocGiangDay = new List<MonHocDTO>();
            if (id != null && !id.Equals(Guid.Empty))
            {
                thongtinGiangVienViewModel.ID = (Guid)id;
                thongtinGiangVienViewModel = thongtinGiangVienViewModel.FromGiangVienDTO(_giangVienReportService.GetGiangVienById((Guid)id));
                thongtinGiangVienViewModel.DanhSachMonHocGiangDay = _giangVienReportService.GetAllMonHocByGiangVienId(thongtinGiangVienViewModel.ID);
            }
            thongtinGiangVienViewModel.DanhSachHocVi = _giangVienReportService.GetAllHocVi();
            thongtinGiangVienViewModel.DanhSachHocHam = _giangVienReportService.GetAllHocHam();
            thongtinGiangVienViewModel.DanhSachChucVu = _giangVienReportService.GetAllChucVu();
            return View(thongtinGiangVienViewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult LopMonHoc()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            ThongTinGiangVienClientViewModel thongTinLopMonHocViewModel = new ThongTinGiangVienClientViewModel();
            if (id != null && !id.Equals(Guid.Empty)) { 
                Guid mahk;
                thongTinLopMonHocViewModel.ID = (Guid)id;
                string strMaHK = _thamSoService.GetThamSo(ThamSoConstantUtility.HOCKYTIEPTHEO);
                Guid.TryParse(strMaHK,out mahk);
                thongTinLopMonHocViewModel.HocKyHienTai = _hocKyReportService.GetHocKyById((Guid)mahk);
                thongTinLopMonHocViewModel.NamHocHienTai = _hocKyReportService.GetNamHocById(thongTinLopMonHocViewModel.HocKyHienTai.MaNamHoc);
                thongTinLopMonHocViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy((Guid)id, (Guid)mahk, null, null, true);
            }
            return View(thongTinLopMonHocViewModel);
        }

        [HttpPost]
        public ActionResult LopMonHoc(Guid lopHocId)
        {
            bool isSuccess = true;
            String mess = "Thao tác thành công!";

            string duocphepxacnhan=_thamSoService.GetThamSo("ChoPhepXacNhanLop");
            bool duocphep = (duocphepxacnhan == null ? false : bool.Parse(duocphepxacnhan));
            if (!duocphep)
            {
                isSuccess = false;
                mess = "Chưa được phép/Đã hết hạn xác nhận lớp học!";
                return Json(new { success = isSuccess, message = mess });
            }            
            try
            {
                Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
                ThongTinGiangVienClientViewModel thongTinLopMonHocViewModel = new ThongTinGiangVienClientViewModel();
                if (id != null && !id.Equals(Guid.Empty))
                {
                    LopMonHocDTO lmhDTO = _lopMonHocReportService.GetLopMonHocById(lopHocId);
                    if (lmhDTO.GiangVienXacNhan)
                    {
                        isSuccess = false;
                        mess = "Không được gửi kiến nghị khi đã xác nhận!Vui lòng liên hệ admin!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                    else
                    {
                        lmhDTO.GiangVienXacNhan = (lmhDTO.GiangVienXacNhan == true ? false : true);
                        lmhDTO.DaPhanHoi = true;
                        if (_lopMonHocCommandService.UpdateLopMonHoc(lmhDTO) == null)
                        {
                            isSuccess = false;
                            mess = "Thao tác không thành công!";
                            return Json(new { success = isSuccess, message = mess });
                        }
                    }

                    //Guid mhk;
                    //thongTinLopMonHocViewModel.ID = (Guid)id;
                    //string strMaHK = _thamSoService.GetThamSo("HocKyHienTai");
                    //Guid.TryParse(strMaHK, out mhk);
                    //thongTinLopMonHocViewModel.HocKyHienTai = _hocKyReportService.GetHocKyById((Guid)mhk);
                    //thongTinLopMonHocViewModel.NamHocHienTai = _hocKyReportService.GetNamHocById(thongTinLopMonHocViewModel.HocKyHienTai.MaNamHoc);
                    //thongTinLopMonHocViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy((Guid)id, (Guid)mhk, null, null, true);

                    //Guid maHocKy;
                    //Guid.TryParse(maHK, out maHocKy);

                    //foreach (var lopmonhoc in thongTinLopMonHocViewModel.DanhSachLopMonHoc)
                    //{
                    //    if (lopmonhoc.MaLopMonHoc == maLMH && lopmonhoc.MaHocKy == maHocKy)
                    //    {
                    //        if (lopmonhoc.GiangVienXacNhan != true)
                    //        {
                    //            lopmonhoc.GiangVienXacNhan = isChecked;
                    //        }
                    //        lopmonhoc.GhiChu = ghiChu;
                    //        lopmonhoc.DaPhanHoi = true;
                    //        LopMonHocDTO a = _lopMonHocCommandService.UpdateLopMonHoc(lopmonhoc);
                    //        isSuccess = true;
                    //    }
                    //}

                }
                else
                {
                    mess = "Vui lòng đăng nhập để thực hiện chức năng này!";
                    return Json(new { success = isSuccess, message = mess });
                }
            }
            catch (Exception exception)
            {
                log.Error(exception.Message, exception);
            }
            return Json(new { success = isSuccess, message = mess });
        }
        [HttpPost]
        public ActionResult KienNghiLopMonHoc(Guid lopHocId,string ghiChu)
        {
            bool isSuccess = true;
            String mess = "Kiến nghị thành công!";
            try
            {
                Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
                ThongTinGiangVienClientViewModel thongTinLopMonHocViewModel = new ThongTinGiangVienClientViewModel();
                if (id != null && !id.Equals(Guid.Empty))
                {                   
                    LopMonHocDTO lmhDTO = _lopMonHocReportService.GetLopMonHocById(lopHocId);
                    if (lmhDTO.GiangVienXacNhan)
                    {
                        isSuccess = false;
                        mess = "Không được gửi kiến nghị khi đã xác nhận!Vui lòng liên hệ admin!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                    else
                    {
                        lmhDTO.GhiChu = ghiChu;
                        lmhDTO.DaPhanHoi = true;
                        if (_lopMonHocCommandService.UpdateLopMonHoc(lmhDTO) == null)
                        {
                            isSuccess = false;
                            mess = "Thao tác không thành công!";
                            return Json(new { success = isSuccess, message = mess });
                        }
                    }
                }
                else
                {
                    mess = "Vui lòng đăng nhập để thực hiện chức năng này!";
                    return Json(new { success = isSuccess, message = mess });
                }
            }
            catch (Exception exception)
            {
                log.Error(exception.Message, exception);
            }
            return Json(new { success = isSuccess, message=mess });
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DangKyGioDay()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            Guid mhk;
            string strMaHK = _thamSoService.GetThamSo("HocKyHienTai");
            Guid.TryParse(strMaHK, out mhk);
            DangKyGioDayGiangVienClientViewModel dangKyGioDayViewModel = new DangKyGioDayGiangVienClientViewModel();
            dangKyGioDayViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVien((Guid)id, true, true, true);
            dangKyGioDayViewModel.DanhSachDangKyGioDay = _dangKyGioDayReportService.GetAllThongTinDangKyGioDay((Guid)id, null, null , null, null, mhk);
            var danhSachTietHoc = new List<TietHocDTO>();
            danhSachTietHoc.Add(new TietHocDTO{
                MaTiet = -1
            });
            danhSachTietHoc.AddRange(_tietHocService.GetAllTietHoc(null, null, null));
            
            dangKyGioDayViewModel.DanhSachTietHoc = danhSachTietHoc;
            dangKyGioDayViewModel.DanhSachCa = _tietHocService.GetAllCa();
            //return View(dangKyGioDayViewModel);
            return View("DangKyGioDayNew", dangKyGioDayViewModel);
        }

        [HttpPost]
        public ActionResult DangKyGioDayNew(DangKyGioDayGiangVienClientViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.MaTietDau == -1 && model.MaTietSau == -1)
                {
                    TietHocDTO tietDau, tietSau;
                    if (model.Ca == -1)
                    {
                        if (model.Buoi != null)
                            _tietHocService.LayTietHocTheoBuoi((bool)model.Buoi, out tietDau, out tietSau);
                        else
                            return Json(new
                            {
                                success = false,
                                message = "Chưa chọn thời gian cho lớp này."
                            });
                    }
                    else
                    {
                        _tietHocService.LayTietHocTheoCa((int)model.Ca, out tietDau, out tietSau);
                    }
                    model.MaTietDau = tietDau.MaTiet;
                    model.MaTietSau = tietSau.MaTiet;
                    model.TietDauDTO = tietDau;
                    model.TietSauDTO = tietSau;
                }
                else
                    if (model.MaTietDau == -1 || model.MaTietSau == -1)
                    {
                        return Json(new
                        {
                            success = false,
                            message = "Chưa chọn thời gian cho lớp này."
                        });
                    }

                try
                {
                    model.GiangVienId = UserUtility.GetCurrentUser().MaNguoiDung;
                    model.TietDauDTO = _tietHocService.GetTietHocByMaTietHoc(model.MaTietDau);
                    model.TietSauDTO = _tietHocService.GetTietHocByMaTietHoc(model.MaTietSau);
                    model.MaHocKy = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai"));
                    _dangKyGioDayCommandService.InsertThongTinDangKyGioDay(model.ToDangKyGioDayDTO());
                }
                catch (Exception exception)
                {
                    log.Error(exception.Message, exception);
                    return Json(new
                    {
                        success = false,
                        message = "Đăng ký không thành công."
                    });
                }
                return Json(new
                {
                    success = true
                });
            }
            else
                return Json(new
                {
                    success = false,
                    message = "Đăng ký không thành công."
                });
        }

        [HttpGet]
        public JsonResult LayThongTinDangKyGioRanh()
        {
            try
            {
                NguoiDungDTO myInfo = new NguoiDungDTO();
                if (Session["UserInfo"] != null)
                {
                    myInfo = (NguoiDungDTO)Session["UserInfo"];
                }
                else
                {
                    throw new Exception("Chưa đăng nhập.");
                }
                if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
                {
                    throw new Exception("Không có quyền này.");
                }
                int[] dsLichRanh = _giangVienReportService.GetAllLichRanh(myInfo.MaNguoiDung, null, null).Select(lichRanh => lichRanh.ThuTu).ToArray();
                return Json(new
                {
                    success = true,
                    list = dsLichRanh
                }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception exception)
            {
                return Json(new { 
                    success = false,
                    message = exception.Message
                }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult LuuDangKyGioRanh(String danhSachDK)
        {
            try
            {
                NguoiDungDTO myInfo = new NguoiDungDTO();
                if (Session["UserInfo"] != null)
                {
                    myInfo = (NguoiDungDTO)Session["UserInfo"];
                }
                else
                {
                    throw new Exception("Chưa đăng nhập.");
                }
                if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
                {
                    throw new Exception("Không có quyền này.");
                }
                _giangVienCommandService.InsertLichRanhByList(myInfo.MaNguoiDung,danhSachDK);
                return Json(new { 
                    success = true
                });
            }
            catch (Exception exception)
            {
                log.Error(exception.Message, exception);
                return Json(new { 
                    success = false,
                    message = exception.Message
                });
            }
        }

        public ActionResult DangKyGioDay(String malopmonhoc, short thu, String maca, String douutien, bool isCheckExisted)
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/SinhVien/Index");
            }
            Guid idgv = myInfo.MaNguoiDung;
            bool isSuccess = true;
            string mess = "Thành công!";            
            Guid mhk;
            int mCa;
            Guid maLopMonHoc;
            Int32 doUuTien;
            try
            {
                Guid.TryParse(malopmonhoc, out maLopMonHoc);
                Int32.TryParse(maca, out mCa);
                doUuTien = Convert.ToInt32(douutien);
            }
            catch
            {
                isSuccess = false;
                mess = "Không hợp lệ!";
                return Json(new { success = isSuccess, message = mess });
            }
            string strMaHK = _thamSoService.GetThamSo("HocKyHienTai");
            Guid.TryParse(strMaHK, out mhk);

            DangKyGioDayDTO dangky = new DangKyGioDayDTO();
            dangky.MaTietDau = mCa;
            dangky.MaLopMonHoc = maLopMonHoc;
            dangky.GiangVienId = idgv;
            dangky.Thu = thu;
            dangky.DoUuTien = (short)doUuTien;
            if ((short)doUuTien == 1) dangky.DuocChon = true;
            dangky.MaHocKy = mhk;
            DangKyGioDayDTO temp=new DangKyGioDayDTO();
            DangKyGioDayGiangVienClientViewModel dangKyGioDayViewModel = new DangKyGioDayGiangVienClientViewModel();
            dangKyGioDayViewModel.DanhSachDangKyGioDay = _dangKyGioDayReportService.GetAllThongTinDangKyGioDay((Guid)idgv, null, null, null, null, mhk);

            if (!isCheckExisted)
            {
                bool isInsert = true;
                foreach (var thongtindk in dangKyGioDayViewModel.DanhSachDangKyGioDay)
                {
                    if (thongtindk.MaLopMonHoc == maLopMonHoc)
                    {
                        if (thongtindk.DoUuTien == doUuTien)
                        {
                            mess = "Đã tồn tại độ ưu tiên: " +thongtindk.DoUuTien+ " của lớp: " +thongtindk.LopMonHocDTO.MonHoc.TenMonHoc+"!";
                            isSuccess = false;
                            isInsert = false;
                            break;
                        }
                        else if (thongtindk.Thu == thu && thongtindk.MaTietDau == mCa)
                        {
                            mess = "Đã tồn tại ĐK lớp: "+thongtindk.LopMonHocDTO.MonHoc.TenMonHoc+" vào thứ:"+thu+" Ca: "+thongtindk.TietDauDTO.TietThu+"!";
                            isSuccess = false;
                            isInsert = false;
                            break;
                        }
                        
                        //mess = "Đã Tồn Tại Đăng Ký Này!";
                        
                    }
                }
                if (isInsert)
                {
                    temp = _dangKyGioDayCommandService.InsertThongTinDangKyGioDay(dangky);
                    if (temp.MaThongTinGiangDay == null)
                    {
                        isSuccess = false;
                    }
                    else
                    {
                        mess = "Thêm Thành Công";
                    }
                }
                //else
                //{
                //    isSuccess = false;
                //    mess = "Đã Tồn Tại Đăng Ký Này!";
                //}
            }
            else
            {                
                foreach (var thongtindk in dangKyGioDayViewModel.DanhSachDangKyGioDay)
                {
                    //if (thongtindk.MaLopMonHoc == maLopMonHoc && thongtindk.Thu == thu && thongtindk.MaCa == mCa && thongtindk.DoUuTien == doUuTien)
                    //{
                    //    isSuccess = false;
                    //    mess = "Đã Tồn Tại Đăng Ký Này!";
                    //    break;
                    //}
                    if (thongtindk.MaLopMonHoc == maLopMonHoc)
                    {
                        if (thongtindk.DoUuTien == doUuTien)
                        {
                            isSuccess = false;
                            mess = "Đã tồn tại độ ưu tiên: " + thongtindk.DoUuTien + " của lớp: " + thongtindk.LopMonHocDTO.MonHoc.TenMonHoc + "!";
                            break;
                        }
                        //else if (thongtindk.Thu == thu && thongtindk.MaCa == mCa)
                        //{
                        //    isSuccess = false;
                        //    mess = "Đã tồn tại ĐK lớp: " + thongtindk.LopMonHocDTO.MonHoc.TenMonHoc + " vào thứ:" + thu + " Ca: " + thongtindk.CaDTO.TenCa + "!";
                        //    break;
                        //}                                               
                    }
                }
            }
            return Json(new { success = isSuccess, message= mess});
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult LayThongTinDangKyGioDay()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            Guid mhk;
            string strMaHK = _thamSoService.GetThamSo("HocKyHienTai");
            Guid.TryParse(strMaHK, out mhk);
            DangKyGioDayGiangVienClientViewModel dangKyGioDayViewModel = new DangKyGioDayGiangVienClientViewModel();
            dangKyGioDayViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVien((Guid)id, true, true, true);
            dangKyGioDayViewModel.DanhSachDangKyGioDay = _dangKyGioDayReportService.GetAllThongTinDangKyGioDay((Guid)id, null, null, null, null, mhk);
            dangKyGioDayViewModel.DanhSachTietHoc = _tietHocService.GetAllTietHoc(null, null, null);
            return View(dangKyGioDayViewModel);
        }

        [HttpPost]
        [GiangVienAuthorize(ReturnJson=true)]
        public ActionResult XoaThongTinDangKyGioDay(String thongtindkID)
        {
            bool isSuccess = true;
            Guid thongtinDKID=Guid.Parse(thongtindkID);
            if (!_dangKyGioDayCommandService.DeleteThongTinDangKyGioDayById(thongtinDKID))
            {
                isSuccess = false;
            }
            return Json(new { success = isSuccess });
        }

        [HttpPost]
        public ActionResult DangXuat()
        {
            Session["UserInfo"] = null;
            String url="/DangNhapClient/GiangVienDangNhap";
            return Json(new { success = url });
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DoiMatKhau()
        {
            //NguoiDungDTO newInfo = _nguoiDungCommandService.DoiMatKhauNguoiDung(myInfo);            
            NguoiDungViewModel nguoiDungViewModel = new NguoiDungViewModel();
            nguoiDungViewModel.TaiKhoanDangNhap = UserUtility.GetCurrentUser().TaiKhoanDangNhap;
            return View(nguoiDungViewModel);
        }

        [HttpPost]
        public ActionResult DoiMatKhau(String matKhauCu,String matKhauMoi,String matKhauMoi2)
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/SinhVien/Index");
            }
            bool isSuccess = true;
            String mess = "Đổi mật khẩu thành công!";
            if (matKhauCu == "" || matKhauMoi == "" || matKhauMoi2 == "")
            {
                isSuccess = false;
                mess = "Vui lòng điền đầy đủ thông tin bắt buộc (*)!";
                return Json(new { success = isSuccess, message=mess });
            }
            if (matKhauCu != myInfo.MatKhau.Trim())
            {
                isSuccess = false;
                mess = "Mật khẩu cũ không trùng khớp!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (matKhauMoi != matKhauMoi2)
            {
                isSuccess = false;
                mess = "Mật khẩu mới không trùng khớp!";
                return Json(new { success = isSuccess, message = mess });
            }
            myInfo.MatKhauMoi = matKhauMoi.Trim();
            NguoiDungDTO result = _nguoiDungCommandService.DoiMatKhauNguoiDung(myInfo);
            if (result.MatKhau.Trim() != matKhauMoi)
            {
                isSuccess = false;
                mess = "Cập nhật thất bại!";                
            }
            Session["UserInfo"] = result;
            return Json(new { success = isSuccess, message = mess });
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DeTai()
        {
            var nguoiDung = UserUtility.GetCurrentUser();

            DeTaiGiangVienClientViewModel deTaiGVViewModel = new DeTaiGiangVienClientViewModel();
            Guid hocKyHienTai=Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            HocKyDTO hocKyDTO = new HocKyDTO();
            Guid MaNamHoc = _hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;
            deTaiGVViewModel.namHoc = _hocKyReportService.GetNamHocById(MaNamHoc);
            //IEnumerable<DeTaiDTO> deTaiGiangVien = _deTaiReportService.GetAllDeTaiGiangVien(nguoiDung.MaNguoiDung, hocKyHienTai,false); //get theo mahocky
            IEnumerable<DeTaiDTO> deTaiGiangVien = _deTaiReportService.GetAllDeTaiGiangVien(nguoiDung.MaNguoiDung, hocKyHienTai, null); //get theo mahocky
            if (deTaiGiangVien == null) deTaiGiangVien = new List<DeTaiDTO>();
            deTaiGVViewModel.GiangVien1 = _giangVienReportService.GetGiangVienById(nguoiDung.MaNguoiDung);
            deTaiGVViewModel.deTaiGiangVien = deTaiGiangVien;
            return View(deTaiGVViewModel);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult DeTai(String TenDeTai,String MoTa)
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            bool isSuccess = true;
            String mess = "Cập nhật thành công!";

            string duocphepdk = _thamSoService.GetThamSo("ChoPhepDangKyDeTai");
            bool duocphep = (duocphepdk == null ? false : bool.Parse(duocphepdk));
            if (!duocphep)
            {
                isSuccess = false;
                mess = "Chưa được phép/Đã hết hạn đăng ký đề tài!";
                return Json(new { success = isSuccess, message = mess });
            }           

            if (TenDeTai.Trim().Length < 1 || MoTa.Trim().Length < 1)
            {
                isSuccess = false;
                mess = "Tên đề tài và mô tả không được rỗng!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            DeTaiGiangVienClientViewModel deTaiGVViewModel = new DeTaiGiangVienClientViewModel();
            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            Guid MaNamHoc=_hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;
            deTaiGVViewModel.namHoc = _hocKyReportService.GetNamHocById(MaNamHoc);
            DeTaiDTO detai = new DeTaiDTO();
            detai.MaDeTai = Guid.NewGuid();
            detai.TenDeTai = TenDeTai;
            detai.MoTa = MoTa;
            detai.KhoaLuanTotNghiep = 0;
            detai.GiangVienHD1 = myInfo.MaNguoiDung;
            detai.GiangVienHD2 = myInfo.MaNguoiDung;
            detai.MaHocKy = _hocKyReportService.GetHocKyById(hocKyHienTai).MaHocKy;
            detai.FileTaiLieu = "";
            DeTaiDTO tem=_deTaiCommandService.InsertDeTai(detai);
            if (tem==null)
            {
                isSuccess = false;
                mess = "Cập nhật thất bại!";
                return Json(new { success = isSuccess, message = mess });
            }
            return Json(new { success = isSuccess, message = mess });
        }
        [HttpPost]
        public ActionResult XoaDeTai(String MaDeTai)
        {
            bool isSuccess = true;
            String mess = "Xóa thành công";

            string duocphepdk = _thamSoService.GetThamSo("ChoPhepDangKyDeTai");
            bool duocphep = (duocphepdk == null ? false : bool.Parse(duocphepdk));
            if (!duocphep)
            {
                isSuccess = false;
                mess = "Chưa được phép/Đã hết hạn huỷ đề tài!";
                return Json(new { success = isSuccess, message = mess });
            }           

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            Guid madetai;
            try {
                madetai = Guid.Parse(MaDeTai);
            }
            catch(Exception ex)
            {
                isSuccess = false;
                mess = "Có lỗi xảy ra! Vui lòng thao tác lại lần nữa!";
                return Json(new { success = isSuccess, message = mess });
            }
            
            
            if (!_deTaiCommandService.DeleteDeTai(madetai))
            {
                isSuccess = false;
            }
            return Json(new { success = isSuccess, message = mess });
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DeNghiMoLop()
        {
            bool isSuccess;
            string message = String.Empty;

            Guid maHocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai"));

            CapNhatLopMonHocViewModel model = new CapNhatLopMonHocViewModel();
            model.MaGiangVien1 = UserUtility.GetCurrentUser().MaNguoiDung;
            model.DanhSachCa = _tietHocService.GetAllCa();
            model.DanhSachMonHoc = _giangVienReportService.GetAllMonHocByGiangVienId(model.MaGiangVien1);
            model.DanhSachLopSinhVien = _lopMonHocReportService.GetAllLopSinhVien();
            model.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy(UserUtility.GetCurrentUser().MaNguoiDung, maHocKyHienTai, null, null, false);
            return View(model);
        }

        [HttpPost]
        public ActionResult DeNghiMoLop(CapNhatLopMonHocViewModel viewModel)
        {
            bool isSuccess = true;
            String mess = "Xóa thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            viewModel.MaGiangVien1 = myInfo.MaNguoiDung;
            viewModel.MaHocKy = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai"));
            viewModel.DongYMoLop = false;
            viewModel.MaLopMonHoc = string.Empty;
            if (ModelState.IsValid)
            {
                try
                {
                    var lopMonHocDTO = viewModel.ToLopMonHocDTO();
                    lopMonHocDTO.MaLopMonHoc = String.Empty;
                    _lopMonHocCommandService.InsertLopMonHoc(lopMonHocDTO);
                    return Json(new
                    {
                        success = true
                    });
                }
                catch (Exception exception)
                {
                    return Json(new { 
                        success = false,
                        message = exception.Message
                    });
                }
            }
            return Json(new
            {
                success = false,
                message = "Kiểm tra lại thông tin."
            });
        }
        [HttpGet]
        public ActionResult DangKyKetThucLopMonHoc()
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/SinhVien/Index");
            }
            DangKyKetThucLopMonHocViewModel dangKyViewModel = new DangKyKetThucLopMonHocViewModel();
            Guid hocKyHienTai=Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            dangKyViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVien(myInfo.MaNguoiDung, true, true, true);
            //IEnumerable<DangKyKetThucLopMonHocDTO> dsdangky = _dangKyKetThucLopMonHocReportService.GetAllDangKyByGiangVien(myInfo.MaNguoiDung, hocKyHienTai);
            //dangKyViewModel = DangKyKetThucLopMonHocViewModel.FromDangKyKetThucLopMonHocDTOList(dsdangky);
            return View(dangKyViewModel);
        }
        [HttpPost]
        public ActionResult DangKyKetThucLopMonHoc(String malopmonhoc, String ngayketthuc)
        {
            bool isSuccess = true;
            String mess = "Đăng ký thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            DateTime ngayKetThucLopMonHoc=new DateTime();
            try
            {
                ngayKetThucLopMonHoc = DateTime.Parse(ngayketthuc);
            }
            catch (Exception ex)
            {
                isSuccess = false;
                mess = "Ngày không hợp lệ!";
                return Json(new { success = isSuccess, message = mess });
            }

            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
//            Guid MaNamHoc = _hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;

            DangKyKetThucLopMonHocDTO dangky = new DangKyKetThucLopMonHocDTO();
            dangky.ID = Guid.NewGuid();
            dangky.GiangVienID = myInfo.MaNguoiDung;
            dangky.MaLopMonHoc = malopmonhoc;
            dangky.NgayKetThucLopMonHoc = ngayKetThucLopMonHoc;
            dangky.NgayDangKy = DateTime.Now;
            dangky.MaHocKy = hocKyHienTai;

            DangKyKetThucLopMonHocDTO result = _dangKyKetThucLopMonHocCommandService.InsertDangKy(dangky);
            if (result == null)
            {
                isSuccess = false;
                mess = "Đăng ký không thành công!";
                return Json(new { success = isSuccess, message = mess });
            }

            return Json(new { success = isSuccess, message = mess });
        }
        public ActionResult LayThongTinDangKyKetThucLopMonHoc()
        {
            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                return Redirect("/DangNhapClient/GiangVienDangNhap");
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                return Redirect("/SinhVien/Index");
            }

            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            
            IEnumerable<DangKyKetThucLopMonHocDTO> dsdk=_dangKyKetThucLopMonHocReportService.GetAllDangKyByGiangVien(myInfo.MaNguoiDung, hocKyHienTai);
            IEnumerable<DangKyKetThucLopMonHocViewModel> danhSachDangKy = null;
            try
            {
                danhSachDangKy = DangKyKetThucLopMonHocViewModel.FromDangKyKetThucLopMonHocDTOList(dsdk);
                return View(danhSachDangKy);
            }
            catch (Exception ex)
            {
                return View(danhSachDangKy);
            }

        }
        
        [HttpPost]
        public ActionResult HuyDangKyKetThucLopMonHoc(Guid id)
        {
            bool isSuccess = true;
            String mess = "Hủy thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }

            try
            {
                _dangKyKetThucLopMonHocCommandService.DeleteDangKy(id);
                return Json(new { success = isSuccess, message = mess });
            }
            catch (Exception ex)
            {
                isSuccess = false;
                mess = "Hủy không thành công!";
                return Json(new { success = isSuccess, message = mess });
            }
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongKeGiangVien()
        {
            var nguoiDung = UserUtility.GetCurrentUser();
            GiangVienViewModel giangVienViewModel = new GiangVienViewModel();
            GiangVienDTO giangVienDTO = _giangVienReportService.GetGiangVienById(nguoiDung.MaNguoiDung);

            giangVienViewModel = GiangVienViewModel.FromGiangvienDTO(giangVienDTO);

            IEnumerable<ChucVuDTO> gvChucVu = _giangVienReportService.GetAllChucVuGiangVien(nguoiDung.MaNguoiDung);
            if (gvChucVu != null)
            {
                giangVienViewModel.DanhSachChucVu = gvChucVu;
            }

            string strMaHK = _thamSoService.GetThamSo("HocKyHienTai");            
            Guid hkHienTai = Guid.Parse(strMaHK);
            giangVienViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy(nguoiDung.MaNguoiDung, hkHienTai, true, null, true);
            giangVienViewModel.HocKyHienTai = _hocKyReportService.GetHocKyById(hkHienTai);
            giangVienViewModel.DanhSachDeTai = _deTaiReportService.GetAllDeTaiGiangVien(nguoiDung.MaNguoiDung,hkHienTai, true);
            giangVienViewModel.DanhSachLopMonHoc=giangVienViewModel.DanhSachLopMonHoc.OrderBy(p => p.LoaiLop);
            giangVienViewModel.DanhSachHocKy = _hocKyReportService.GetAllHocKy().OrderBy(hk => hk.TenHocKy).OrderBy(hk => hk.NamHoc.Nam1);
            return View(giangVienViewModel);
            
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult ThongKeGiangDayGiangVien(Guid mahk)
        {
            var nguoiDung = UserUtility.GetCurrentUser();
            GiangVienViewModel giangVienViewModel = new GiangVienViewModel();
            GiangVienDTO giangVienDTO = _giangVienReportService.GetGiangVienById(nguoiDung.MaNguoiDung);

            giangVienViewModel = GiangVienViewModel.FromGiangvienDTO(giangVienDTO);

            IEnumerable<ChucVuDTO> gvChucVu = _giangVienReportService.GetAllChucVuGiangVien(nguoiDung.MaNguoiDung);
            if (gvChucVu != null)
            {
                giangVienViewModel.DanhSachChucVu = gvChucVu;
            }
            
            Guid hkHienTai = Guid.Empty;
            if (mahk != null && !mahk.Equals(Guid.Empty))
            {
                hkHienTai = mahk;
            }
            else
            {
                string strMaHK = _thamSoService.GetThamSo("HocKyHienTai");
                hkHienTai = Guid.Parse(strMaHK);
            }
            giangVienViewModel.DanhSachLopMonHoc = _lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy(nguoiDung.MaNguoiDung, hkHienTai, true, null, true);
            giangVienViewModel.HocKyHienTai = _hocKyReportService.GetHocKyById(hkHienTai);
            giangVienViewModel.DanhSachDeTai = _deTaiReportService.GetAllDeTaiGiangVien(nguoiDung.MaNguoiDung, hkHienTai, true);
            giangVienViewModel.DanhSachLopMonHoc = giangVienViewModel.DanhSachLopMonHoc.OrderBy(p => p.LoaiLop);
            return View(giangVienViewModel);

        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DanhSachLopMo()
        {
            IEnumerable<LopMonHocViewModel> lopMonHocViewModel = LopMonHocViewModel.FromLopMonHocDTOList(_lopMonHocReportService.GetAllLopMonHocTheoHocKy(new Guid(_thamSoService.GetThamSo("HocKyHienTai")), null, null, true));
            return View(lopMonHocViewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult LichHoc()
        {
            return View();
        }

        [HttpGet]
        [GiangVienAuthorize]
        [HandleError]
        public ActionResult XemLichThi()
        {
            //IEnumerable<LopMonHocViewModel> viewModel = LopMonHocViewModel.FromLopMonHocDTOList(_lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy(UserUtility.GetCurrentUser().MaNguoiDung, _thamSoService.GetGuid(ThamSoConstantUtility.HOCKYHIENTAI), true, true, true));
            var hocKyHienTai = _thamSoService.GetGuid(ThamSoConstantUtility.HOCKYHIENTAI);
            var viewModel = new DanhSachLichThiViewModel();
            viewModel.DanhSachLopMonHoc = LopMonHocViewModel.FromLopMonHocDTOList(_lopMonHocReportService.GetAllLopMonHocTheoGiangVienTrongHocKy(UserUtility.GetCurrentUser().MaNguoiDung, hocKyHienTai, true, true, true));
            viewModel.DanhSachLichThi = _lichThiReportService.GetAllLichThi(hocKyHienTai, UserUtility.GetCurrentUser().MaNguoiDung, null, null);
            viewModel.SortLichThiByLopMonHoc();
            return View(viewModel);
        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult DangKyLichThi(Guid? maLopMonHoc, Guid? maLichThi)
        {
            if (maLopMonHoc != null && maLopMonHoc != Guid.Empty)
            {
                DangKyLichThiViewModel viewModel;
                var lopMonHoc = _lopMonHocReportService.GetLopMonHocById((Guid)maLopMonHoc);

                if (maLichThi != null && maLichThi != Guid.Empty)
                {
                    var lichThiLopMonHoc = _lichThiReportService.GetLichThiById((Guid)maLichThi);
                    viewModel = DangKyLichThiViewModel.FromLichThiDTO(lichThiLopMonHoc);
                }
                else
                {
                    viewModel = new DangKyLichThiViewModel();
                    viewModel.MaLopMonHoc = (Guid)maLopMonHoc;
                    viewModel.LopMonHoc = lopMonHoc;
                }
                return View(viewModel);
            }
            else
                return HttpNotFound();
        }

        [GiangVienAuthorize]
        public ActionResult GiangDay()
        {
            return View();
        }

        [HttpPost]
        [GiangVienAuthorize(ReturnJson=true)]
        public ActionResult DangKyLichThi(DangKyLichThiViewModel viewModel)
        {
            string message = string.Empty;
            bool success = true;

            string duocphepdk = _thamSoService.GetThamSo("ChoPhepDangKyLichThi");
            bool duocphep = (duocphepdk == null ? false : bool.Parse(duocphepdk));
            if (!duocphep)
            {
                success = false;
                message = "Chưa được phép/Đã hết hạn đăng ký lịch thi!";
                return Json(new { success = success, message = message });
            }     

            if (ModelState.IsValid)
            {
                try
                {
                    viewModel.NgayGio = viewModel.GetNgayGio();
                    if (viewModel.HinhThucThi == 1) //Bao Cao
                    {
                        viewModel.ThoiGianLamBai = viewModel.GetThoiGianLamBai();
                        if (viewModel.ThoiGianLamBai < 0)
                        {
                            throw new Exception("Giờ kết thúc phải sau giờ bắt đầu thi.");
                        }
                    }

                    if (viewModel.MaLichThi.Equals(Guid.Empty))
                    {
                        _lichThiCommandService.InsertLichThi(viewModel.ToLichThiDTO());
                    }
                    else
                        _lichThiCommandService.UpdateLichThi(viewModel.ToLichThiDTO());
                }
                catch (Exception exception)
                {
                    log.Error(exception.Message, exception);
                    success = false;
                    message = exception.Message;
                }
            }
            else
            {
                success = false;
                message = "Đăng ký không thành công. Kiểm tra lại dữ liệu nhập.";
            }
            return Json(new { 
                success = success,
                message = message
            });
        }
        public ActionResult BanTinColumn()
        {
            List<BanTinDTO> bantin = new List<BanTinDTO>();
            bantin = _banTinReportService.GetTopNumbersBanTin(10, 1).ToList(); //1 ban tin cho sinh vien, 1: ban tin cho giang vien
            IEnumerable<BanTinGiangVienViewModel> banTinSinhVienViewModel = BanTinGiangVienViewModel.FromBanTinDTOList(bantin);
            return View(banTinSinhVienViewModel);
        }
        public ActionResult ChiTietBanTin(Guid id)
        {
            BanTinDTO bantin = new BanTinDTO();
            bantin = _banTinReportService.GetBanTinById(id);
            BanTinGiangVienViewModel bantinsv = new BanTinGiangVienViewModel();
            if (bantin != null)
            {
                bantinsv = BanTinGiangVienViewModel.FromBanTinDTO(bantin);
            }
            return View(bantinsv);
        }
        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult QuanLyTienDoDeTai()
        {
            var nguoiDung = UserUtility.GetCurrentUser();
            DeTaiGiangVienClientViewModel deTaiGVViewModel = new DeTaiGiangVienClientViewModel();
            Guid hocKyHienTai = Guid.Parse(_thamSoService.GetThamSo("HocKyHienTai").ToString());
            HocKyDTO hocKyDTO = new HocKyDTO();
            Guid MaNamHoc = _hocKyReportService.GetHocKyById(hocKyHienTai).MaNamHoc;
            deTaiGVViewModel.namHoc = _hocKyReportService.GetNamHocById(MaNamHoc);
            IEnumerable<DeTaiDTO> deTaiGiangVien = _deTaiReportService.GetAllDeTai(nguoiDung.MaNguoiDung, null, hocKyHienTai, null, null, true); //get theo mahocky
            
            if (deTaiGiangVien == null) deTaiGiangVien = new List<DeTaiDTO>();
            deTaiGVViewModel.GiangVien1 = _giangVienReportService.GetGiangVienById(nguoiDung.MaNguoiDung);
            deTaiGVViewModel.deTaiGiangVien = deTaiGiangVien;
            return View(deTaiGVViewModel);

        }

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult TienDoDeTai(Guid Id)
        {
            TienDoDeTaiViewModel tienDoDeTaiViewModel = new TienDoDeTaiViewModel();
            tienDoDeTaiViewModel.DanhSachTienDoDT = _tienDoDeTaiReportService.GetAllTienDoDeTai(Id);
            return View(tienDoDeTaiViewModel);
        }
        

        [HttpGet]
        [GiangVienAuthorize]
        public ActionResult AddTienDoDeTai(String Id)
        {
            ViewBag.MaDeTai=Id;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddTienDoDeTai(Guid maDeTai,String tieuDe, String noiDung, String tienDoHoanThanh)
        {
            bool isSuccess = true;
            String mess = "Thêm thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (maDeTai == null || maDeTai.Equals(Guid.Empty) || tieuDe == null || tieuDe == "" || noiDung == null || noiDung == "" || tienDoHoanThanh == null)
            {
                isSuccess = false;
                mess = "Dữ liệu không hợp lệ! Vui lòng kiểm tra lại!";
                return Json(new { success = isSuccess, message = mess });
            
            }
            int tiendoHT = 0;
            try{

                tiendoHT = Int32.Parse(tienDoHoanThanh);
            }
            catch(Exception ex)
            {
                isSuccess = false;
                mess = "Tiến độ phải <= 100 && >=0!";
                return Json(new { success = isSuccess, message = mess });
            }

            if (tiendoHT < 0 || tiendoHT>100)
            {
                isSuccess = false;
                mess = "Tiến độ phải <= 100 && >=0!";
                return Json(new { success = isSuccess, message = mess });
            }
                TienDoDeTaiDTO tienDoDTO = new TienDoDeTaiDTO();
                tienDoDTO.MaTienDoDeTai = Guid.NewGuid();
                tienDoDTO.MaDeTai = maDeTai;
                tienDoDTO.TieuDe = tieuDe;
                tienDoDTO.NoiDung = noiDung;
                tienDoDTO.TienDoHoanThanh = tiendoHT;
                tienDoDTO.NgayBaoCao = DateTime.Now;

                TienDoDeTaiDTO td = _tienDoDeTaiCommandService.InsertTienDoDeTai(tienDoDTO);
                if (td == null)
                {
                    isSuccess = false;
                    mess = "Thêm không thành công!";                    
                }
               return Json(new { success = isSuccess, message = mess });
        }

        [HttpGet]
        public ActionResult CapNhatDeTai(String id)
        {
            Guid maDeTai;
            try
            {
                maDeTai = Guid.Parse(id);
            }
            catch
            {
                DeTaiDTO detaiDTO = null;
                return View(detaiDTO);
            }
            DeTaiDTO detai = _deTaiReportService.GetDeTaiById(maDeTai);
            return View(detai);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CapNhatDeTai(Guid maDeTai, String tenDeTai, String moTa)
        {
            bool isSuccess = true;
            String mess = "Thêm thành công";

            string duocphepdk = _thamSoService.GetThamSo("ChoPhepDangKyDeTai");
            bool duocphep = (duocphepdk == null ? false : bool.Parse(duocphepdk));
            if (!duocphep)
            {
                isSuccess = false;
                mess = "Chưa được phép/Đã hết hạn đăng ký đề tài!";
                return Json(new { success = isSuccess, message = mess });
            }           

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (maDeTai == null || maDeTai.Equals(Guid.Empty) || tenDeTai == null || tenDeTai == "" || moTa == null || moTa == "")
            {
                isSuccess = false;
                mess = "Dữ liệu không hợp lệ! Vui lòng kiểm tra lại!";
                return Json(new { success = isSuccess, message = mess });

            }

            DeTaiDTO detaiDTO = _deTaiReportService.GetDeTaiById(maDeTai);
            if (detaiDTO == null)
            {
                isSuccess = false;
                mess = "Cập nhật không thành công!";
                return Json(new { success = isSuccess, message = mess });
            }
            else
            {
                detaiDTO.TenDeTai = tenDeTai;
                detaiDTO.MoTa = moTa;
                if(_deTaiCommandService.UpdateDeTai(detaiDTO) == null)
                {
                    isSuccess = false;
                    mess = "Cập nhật không thành công!";
                    return Json(new { success = isSuccess, message = mess });
                }
            }
            return Json(new { success = isSuccess, message = mess });

        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult XacNhanDeTai(Guid maDeTai, int khoaLuan)
        {
            bool isSuccess = true;
            String mess = "Cập nhật thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }            
            DeTaiDTO detaiDTO = _deTaiReportService.GetDeTaiById(maDeTai);
            if (detaiDTO == null)
            {
                isSuccess = false;
                mess = "Cập nhật không thành công!";
                return Json(new { success = isSuccess, message = mess });
            }
            else
                {
                detaiDTO.KhoaLuanTotNghiep = (khoaLuan < 2) ? (khoaLuan==1?0:1) : 0;
                    if (_deTaiCommandService.UpdateDeTai(detaiDTO)==null)
                    {
                        isSuccess = false;
                        mess = "Cập nhật không thành công!";
                        return Json(new { success = isSuccess, message = mess });
                    }
                }            
            return Json(new { success = isSuccess, message = mess });
        }

        [HttpPost]
        public ActionResult DeleteTienDoDeTai(Guid maTienDoDeTai)
        {
            bool isSuccess = true;
            String mess = "Xoá thành công";

            NguoiDungDTO myInfo = new NguoiDungDTO();
            if (Session["UserInfo"] != null)
            {
                myInfo = (NguoiDungDTO)Session["UserInfo"];
            }
            else
            {
                isSuccess = false;
                mess = "Vui lòng đăng nhập trước!";
                return Json(new { success = isSuccess, message = mess });
            }
            if (myInfo.VaiTro == 0) // =1 la giang vien; !=1 la sinh vien
            {
                isSuccess = false;
                mess = "Bạn không có quyền truy cập tính năng này!";
                return Json(new { success = isSuccess, message = mess });
            }           

            bool td = _tienDoDeTaiCommandService.DeleteTienDoDeTai(maTienDoDeTai);
            if (td == false)
            {
                isSuccess = false;
                mess = "Xoá không thành công!";
            }
            return Json(new { success = isSuccess, message = mess });
        }

        [HttpGet]
        public ActionResult NhapDiemSinhVien()
        {
            var maGiangVien = UserUtility.GetCurrentUser().MaNguoiDung;
            ViewData["MaGiangVien"] = maGiangVien;
            return View();
        }
        public ActionResult ThongTinGiangVienTopLayout()
        {
            Guid id = UserUtility.GetCurrentUser().MaNguoiDung;
            GiangVienDTO gvDTO=_giangVienReportService.GetGiangVienById(id);
            return View(gvDTO);
        }
    }
}
